<template>
  <!-- <el-select v-model="value" placeholder="请选择" @change="changeValue($event)" clearable >
    <el-option
      v-for="item in partys"
      :key="item.code"
      :label="item.name"
      :value="item.code"
    />
  </el-select> -->
  <!-- <el-autocomplete
    class="inline-input"
    v-model="value"
    :fetch-suggestions="querySearch"
    placeholder="请输入内容"
    @select="changeValue($event)"
    style="width:100%"
    clearable
  >
  </el-autocomplete>             -->
  <el-select v-model="value" filterable placeholder="请选择" @change="changeValue($event)" clearable style="width:100%">
    <el-option
      v-for="item in partys"
      :key="item.partyCode"
      :label="item.userName"
      :value="item.partyCode"
    />
  </el-select>
</template>
<script>
import request from '@/utils/request'
export default {
  props:{
    url: {
      type: String,
      default: "/arpa-basic-api/party/wmsShipment",
    },
    defaultValue: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      value: this.defaultValue,
      partys: [],
      callbackMethod: '',
      partyName: ''
    }
  },
  watch: {
    defaultValue: {
      deep: true,
      immediate: true,
      handler(v) {
        this.value = v;
      }
    }
  },
  created() {
    this.findShipmentPartys()
  },
  methods: {
    findShipmentPartys() {
      request({
        url: this.url,
        method: 'get',
        params: { name: this.partyName }
      }).then(response => {
        this.partys = response.data
        if(this.partys.length === 1){
          this.value = this.partys[0].partyCode
          this.$emit("setCustomerCode", this.partys[0].partyCode)
        }
      })
    },

    // querySearch(queryString, callback) {

    //     this.value = queryString
    //     var list = [{}];
    //     var that = this;
    //     request({
    //       url: '/arpa-basic-api/party/wmsShipment',
    //       method: 'get',
    //       params: { name: queryString }
    //     }).then(response => {
    //       this.partys = response.data
    //     })
    //         .then((response)=>{
    //         for(let i of this.partys){
    //             i.value = i.name;
    //         }
    //         list = this.partys;
    //         callback(list);
    //     }).catch((error)=>{
    //     console.log(error);
    //     })
    // },

    changeValue(e) {
      this.$emit("setCustomerCode", e)
    },
    setSelectValue(value) {
      if(value!=''){
        this.partys.forEach(i =>{
          if(i.code == value){
            this.value = i.name
          }
          if(i.name == value){
            this.value = value
          }
        })
      }else{
        this.value = value
      }
    },
  }
}
</script>
